﻿using DevExpress.Skins;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using DevExpress.Internal;
using DevExpress.WindowsMailClient.Win.Utils;
using DevExpress.XtraWaitForm;
using DevExpress.LookAndFeel;
using System.IO;
using SoftKeyYTWorld;

namespace OPX_FlowGlueMachine
{
    internal static class Program
    {
        const string AppName = "OPX_FlowGlueMachine";
        internal static SkinSetting skinSetting;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            if (USBDogInit() == false)
            {
                MessageBox.Show("没有检测到USBkey！退出,请联系设备负责人");
                return;
            }
            bool exit;
            using (DevAVDataDirectoryHelper.SingleInstanceApplicationGuard(AppName, out exit))
            {
                if (exit) return;
            }
            string strApp = Application.StartupPath + @"\OtherLib\";
            AddEnvironmentPaths(new string[] { strApp });

            skinSetting = SkinSetting.Load();
            WindowsFormsSettings.ScrollUIMode = ScrollUIMode.Touch;
            WindowsFormsSettings.CustomizationFormSnapMode = DevExpress.Utils.Controls.SnapMode.OwnerControl;
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            UserLookAndFeel.Default.SetSkinStyle(Program.skinSetting.UIStyle, Program.skinSetting.UIPaletteName);
            //Application.Run(new FormMain());

            using (new StartUpProcess())
            {
                using (StartUpProcess.Status.Subscribe(new DemoStartUp()))
                {
                    Application.Run(new FormMain());
                    
                }
            }
        }
        internal static bool USBDogInit()
        {
            SoftKeyYT88 ytsoftkey;
            string KeyPath = "";
            ytsoftkey = new SoftKeyYT88();
            //这个用于判断系统中是否存在着加密锁。不需要是指定的加密锁,
            if (ytsoftkey.FindPort(0, ref KeyPath) != 0)
            {
                return false;
            }
            string outstring = "";
            int mylen;
            mylen = 13;//注意这里的6是长度，要与写入的字符串的长度相同
            if (ytsoftkey.YReadString(ref outstring, 0, mylen, "F2BEAC71", "F7D1D127", KeyPath) != 0) { return false; }

            if (outstring == "worldrd123456")
                return true;
            return false;

        }
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            MessageBox.Show(string.Format("捕获到未处理异常：{0}\r\n异常信息：{1}\r\n异常堆栈：{2}\r\nCLR即将退出：{3}", ex.GetType(), ex.ToString(), ex.StackTrace, e.IsTerminating));
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            MessageBox.Show(string.Format("捕获到未处理异常：{0}\r\n异常信息：{1}\r\n异常堆栈：{2}", ex.GetType(), ex.ToString(), ex.StackTrace));
        }
        static void AddEnvironmentPaths(IEnumerable<string> paths)
        {
            var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };

            string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(paths));

            Environment.SetEnvironmentVariable("PATH", newPath);
        }
    }
}